package com.dawn.module.system.controller.app.menu;

import com.dawn.framework.common.enums.CommonStatusEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.module.system.controller.admin.permission.vo.role.RolePageReqVO;
import com.dawn.module.system.controller.app.menu.vo.AppMenuRespVO;
import com.dawn.module.system.controller.app.menu.vo.AppMenuVO;
import com.dawn.module.system.convert.menu.MenuConvert;
import com.dawn.module.system.dal.dataobject.permission.MenuDO;
import com.dawn.module.system.dal.dataobject.permission.RoleDO;
import com.dawn.module.system.enums.permission.AppMenuPositionEnum;
import com.dawn.module.system.service.permission.MenuService;
import com.dawn.module.system.service.permission.PermissionService;
import com.dawn.module.system.service.permission.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static com.dawn.framework.common.pojo.CommonResult.success;
import static com.dawn.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 App - 字典数据")
@RestController
@RequestMapping("/system/menu")
@Validated
public class AppMenuController {

    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private MenuService menuService;


    @GetMapping("/get-menu")
    @Operation(summary = "获取移动端登录用户的权限信息")
    public CommonResult<AppMenuRespVO> getMenuList(@RequestParam("roleCode") String roleCode) {
        // 1.2 获得角色列表
        RolePageReqVO rolePageReqVO = new RolePageReqVO();
        rolePageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        rolePageReqVO.setCode(roleCode);
        PageResult<RoleDO> rolePage = roleService.getRolePage(rolePageReqVO);
        if(rolePage.getTotal() <= 0){
            return success(null);
        }
        List<RoleDO> roles = rolePage.getList();
        roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色

        // 1.3 获得菜单列表
        Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId));
        List<MenuDO> menuList = menuService.getMenuList(menuIds);
        menuList.removeIf(menu -> !CommonStatusEnum.ENABLE.getStatus().equals(menu.getStatus())); // 移除禁用的菜单

        // 2. 拼接结果返回
        List<AppMenuVO> appMenuList = MenuConvert.INSTANCE.convert(menuList);

        AppMenuRespVO appMenuRespVO = new AppMenuRespVO();
        for (AppMenuVO appMenuVO : appMenuList) {
            for (AppMenuVO child : appMenuVO.getChildren()) {
                if(AppMenuPositionEnum.INDEX_TOP.getCode().equals(child.getColor())){
                    appMenuRespVO.setIndexTop(child.getChildren());
                } else if (AppMenuPositionEnum.INDEX_MIDDLE.getCode().equals(child.getColor())) {
                    appMenuRespVO.setIndexMiddle(child.getChildren());
                } else if (AppMenuPositionEnum.MINE_MIDDLE.getCode().equals(child.getColor())) {
                    appMenuRespVO.setMineMiddle(child.getChildren());
                } else if (AppMenuPositionEnum.MINE_FOOT.getCode().equals(child.getColor())) {
                    appMenuRespVO.setMineFoot(child.getChildren());
                }
            }
        }
        return success(appMenuRespVO);
    }

}
